package com.lyy.lottery.controller;

import com.lyy.lottery.common.errorcode.ControllerErrorCodeConstants;
import com.lyy.lottery.common.exception.ControllerException;
import com.lyy.lottery.common.pojo.CommonResult;
import com.lyy.lottery.common.util.JacksonUtil;
import com.lyy.lottery.controller.param.CreatePrizeParam;
import com.lyy.lottery.controller.param.PageParam;
import com.lyy.lottery.controller.result.PrizeListResult;
import com.lyy.lottery.service.PictureService;
import com.lyy.lottery.service.PrizeService;
import com.lyy.lottery.service.dto.PrizeDTO;
import com.lyy.lottery.service.dto.PageListDTO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestPart;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.validation.Valid;
import java.util.stream.Collectors;

@RestController
@Slf4j
public class PrizeController {
    @Autowired
    private PictureService pictureService;
    @Autowired
    private PrizeService prizeService;

    @RequestMapping("/pic/upload")
    public String upload(MultipartFile file){
        log.info("MultipartFile:{}",file);
        return pictureService.savePicture(file);
    }

    /**
     * 创建奖品
     * @RequestPart：用于接收表单数据的 multipart/form-data
     * @param param
     * @param picfile
     * @return
     *
     */
    @RequestMapping("/prize/create")
    public CommonResult<Long> createPrize(@Validated @RequestPart("param") CreatePrizeParam param, @RequestPart("prizePic") MultipartFile picfile){
        log.info("createPrize param:{}", JacksonUtil.writeValueAsString(param));
        return CommonResult.sucess(prizeService.createPrize(param,picfile));
    }

    @RequestMapping("/prize/find-list")
    public CommonResult<PrizeListResult> findPrizeList(PageParam param){
        log.info("findPrizeList PageParam:{}",JacksonUtil.writeValueAsString(param));
        PageListDTO<PrizeDTO> pageListDTO =prizeService.findPrizeList(param);
        return CommonResult.sucess(convertToPrizeListResult(pageListDTO));
    }

    private PrizeListResult convertToPrizeListResult(PageListDTO<PrizeDTO> pageListDTO) {
        if (null == pageListDTO){
            throw new ControllerException(ControllerErrorCodeConstants.FIND_PRIZE_ERROR);
        }

        PrizeListResult result=new PrizeListResult();
        result.setTotal(pageListDTO.getTotal());
        result.setRecords(
                pageListDTO.getRecords().stream()
                        .map(prizeDTO -> {
                            PrizeListResult.PrizeInfo prizeInfo=new PrizeListResult.PrizeInfo();
                            prizeInfo.setPrizeId(prizeDTO.getPrizeId());
                            prizeInfo.setPrizeName(prizeDTO.getName());
                            prizeInfo.setDescription(prizeDTO.getDescription());
                            prizeInfo.setPrice(prizeDTO.getPrice());
                            prizeInfo.setImageUrl(prizeDTO.getImageUrl());
                            return prizeInfo;
                        }).collect(Collectors.toList())
        );
        return result;
    }
}


















